﻿using System;
using System.Web.UI.WebControls;
using School.Entities;
using School.DAL;


public partial class UserManagement_UserDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["UserID"] == null)
            {
                fmvUser.ChangeMode(System.Web.UI.WebControls.FormViewMode.Insert);
            }
        }

        if (Request.QueryString["UserID"] != null)
        {
            ddlMissingRoles.Visible = new UserController().IsAllUserInRoles(int.Parse(Request.QueryString["UserID"].ToString()));
            btnAddToRole.Visible = ddlMissingRoles.Visible;
        }
    }

    /*************************************************************
     * Обробник події на клік по кнопці.
     * Видаляє елемент даних з контрола FormView викликаючи
     * для видалення метод, що прописаний в джерелі даних ObjectDataSource
     * ************************************************************/
    public void btnDeleteUser_Click(object sender, EventArgs e)
    {
        fmvUser.DeleteItem();
        Response.Redirect("~/UserManagement/Users.aspx");
    }

    public void btnInsert_Insert_Click(object sender, EventArgs e)
    {
        fmvUser.InsertItem(true);
        Response.Redirect("~/UserManagement/Users.aspx");
    }

    public void btnAddToRole_Click(object sender, EventArgs e)
    {
        new UserController().AddToRole((int)fmvUser.DataKey.Value, int.Parse(ddlMissingRoles.SelectedValue));

        grvUserRole.DataBind();
        ddlMissingRoles.DataBind();
        ddlMissingRoles.Visible = ddlMissingRoles.Items.Count > 0;
        btnAddToRole.Visible = ddlMissingRoles.Visible;
        Response.Redirect(Request.RawUrl);
    }

    protected void grvUserRole_RowDeleted(object sender, System.Web.UI.WebControls.GridViewDeletedEventArgs e)
    {
        ddlMissingRoles.DataBind();
        ddlMissingRoles.Visible = ddlMissingRoles.Items.Count > 0;
        btnAddToRole.Visible = ddlMissingRoles.Visible;
        Response.Redirect(Request.RawUrl);
    }

    protected void btnAddUserInExtWork_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["UserID"] != null)
        {
            UserInEW ew = new UserInEW()
            {
                UserID = int.Parse(Request.QueryString["UserID"].ToString()),
                EWID = int.Parse(ddlEWName.SelectedValue),
                ClassID = int.Parse(ddlClassEW.SelectedValue),
                SubjectID = int.Parse(ddlSubjectEW.SelectedValue),
                Price = Decimal.Parse(tbPriceEW.Text.Replace('.', ','))
            };
            if (!ddlSubjectEW.Enabled) ew.SubjectID = null;

            new WorkController().InsertUserInExtensionWorkItem(ew);
            gvExtensionWork.DataBind();
            Response.Redirect(Request.RawUrl);
        }
    }

    protected void btnAddToTabelLoad_Click(object sender, EventArgs e)
    {
        TabelLoad tl = new TabelLoad()
        {
            UserID      = (int)fmvUser.DataKey.Value,
            ClassID     = int.Parse(ddlClass.SelectedValue),
            SubjectID   = int.Parse(ddlSubject.SelectedValue),
            Hour        = Decimal.Parse(tbHour.Text.Replace('.',',')),
            Price       = Decimal.Parse(tbPrice.Text.Replace('.', ','))
        };

        new UserController().InsertTabelLoadItem(tl);
        gvTabelLoad.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void grvTabelLoad_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        ddlSubject.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void gvExtWork_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gvExtensionWork.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void gvExtensionWork_RowDeleting(object sender, EventArgs e)
    {
        LinkButton lb = sender as LinkButton;
        if (lb.CommandArgument != null)
            new WorkController().DeleteUserInExtensionWorkItem(int.Parse(lb.CommandArgument));
        gvExtensionWork.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void grvTableLoad_RowDeleting(object sender, EventArgs e)
    {
        LinkButton lb = sender as LinkButton;
        if (lb.CommandArgument != null)
            new UserController().DeleteSubject(int.Parse(lb.CommandArgument));
        gvTabelLoad.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void ddlEWName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlEWName.SelectedItem.Text.Equals("Перевірка зошитів"))
        {
            ddlSubjectEW.Enabled = true;
        }
        else
        {
            ddlSubjectEW.Enabled = false;
        }
    }

    protected void BtnAddVidpuskni_Click(object sender, EventArgs e)
    {
        new WorkController().InsertUserInCharge(new UserInCharge
        {
           UserID = (int)fmvUser.DataKey.Value,
           ChargesID = new WorkController().GetChargeIdByName(lblVidp.Text),
           HourCount = Convert.ToDecimal(tbHourVidp.Text.ToString()),
           Perc = Convert.ToDecimal(tbPercentVidp.Text.ToString()),
        });
        tbPercentVidp.Text = "";
        tbHourVidp.Text = "";
        grvCharge.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void BtnAddLikarnjani_Click(object sender, EventArgs e)
    {
        new WorkController().InsertUserInCharge(new UserInCharge
        {
            UserID = (int)fmvUser.DataKey.Value,
            ChargesID = new WorkController().GetChargeIdByName(lblLikarn.Text),
            HourCount = Convert.ToDecimal(tbHourLikarn.Text.ToString()),
            Perc = Convert.ToDecimal(tbPercentLikarn.Text.ToString()),
        });
        tbPercentLikarn.Text = "";
        tbHourLikarn.Text = "";
        grvCharge.DataBind();
        Response.Redirect(Request.RawUrl);
    }

    protected void BtnAddPremija_Click(object sender, EventArgs e)
    {
        new WorkController().InsertUserInCharge(new UserInCharge
        {
            UserID = (int)fmvUser.DataKey.Value,
            ChargesID = new WorkController().GetChargeIdByName(lblPremija.Text),
            Price = Convert.ToDecimal(tbSummPremija.Text.ToString()),
        });
        tbSummPremija.Text = "";
        grvCharge.DataBind();
        Response.Redirect(Request.RawUrl);
    }
    
}
